package org.kernely.core.service;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

/**
 * Extends this class allows to define our own install and update method.
 */
public abstract class KernelyActivator implements BundleActivator {
	
	/**
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		ServiceReference[] refs = context.getServiceReferences(BundlePresentService.class.getName(), "(Language=*)");
		if(refs != null){
			BundlePresentService bundleChecker = (BundlePresentService) context.getService(refs[0]);
			if(bundleChecker.checkBundle(context.getBundle())){
				if(bundleChecker.getBundleVersion(context.getBundle()).equals(context.getBundle().getVersion().toString())){
					this.startBundle();
				}
				else{
					this.updateBundle();
				}
			}
			else
			{
				this.installBundle();
				this.startBundle();
			}
		}
		
		
	}

	public void stop(BundleContext context) throws Exception {
		
	}
	/**
	 * Method called when the bundle is installing.
	 */
	public abstract void installBundle();

	/**
	 * Method called when the bundle is starting.
	 */
	public abstract void startBundle();
	
	/**
	 * Method called when the bundle is updating.
	 */
	public abstract void updateBundle();
	
	
}
